﻿using Futajma.MigrationStudio.LiveLinkImporter.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Futajma.MigrationStudio.LiveLinkImporter.DeploymentReport
{
    public class DeploymentReportImportStatus
    {
        public string SourceUrl {get;set;}
        public string Message {get;set;}
    }


    public class DeploymentReportImporter
    {
        #region DeploymentReport Import

        public void ImportDeploymentReport(string filename, TreeView tree, int currentProjectVersion, List<DeploymentReportImportStatus> statusList)
        {
            var flatTreeNodes = new Dictionary<string, TreeNode>();

            ItemLoader loader = new ItemLoader();
            loader.LoadReport(filename);

            foreach (TreeNode node in tree.Nodes)
            {
                PopulateRecursive(node, flatTreeNodes);
            }

            foreach (var key in loader.Keys)
            {
                if (flatTreeNodes.ContainsKey(key))
                {
                    var item = loader[key];
                    var node = flatTreeNodes[key];

                    var data = node.Tag as DataNode;

                    if (!data.CommitedItem && data.Version == currentProjectVersion)
                    {
                        if (data.SharePointType == DataDefinition.SharePointType.Document ||
                            data.SharePointType == DataDefinition.SharePointType.Item ||
                            data.SharePointType == DataDefinition.SharePointType.Folder)
                        {
                            if (data.Title == item.SourceDisplayName)
                            {
                                statusList.Add(new DeploymentReportImportStatus() { Message = string.Format("Title changed : {0} to {1}", data.Title, item.NewDisplayName), SourceUrl = item.SourceUrl });
                                data.Title = node.Text = item.NewDisplayName;
                            }
                            else
                            {
                                statusList.Add(new DeploymentReportImportStatus() { Message = string.Format("Error; SourceDisplayname not equals current title, renamed in the meantime: [{0}]=[{1}]", item.SourceDisplayName, data.Title) , SourceUrl = item.SourceUrl });
                            }
                        }
                        else if (data.SharePointType == DataDefinition.SharePointType.DocLibrary)
                        {
                            if (data.TargetPath == item.SourceDisplayName)
                            {                                
                                statusList.Add(new DeploymentReportImportStatus() { Message = string.Format("Path changed : {0} to {1}", data.TargetPath, item.NewDisplayName) , SourceUrl = item.SourceUrl });
                                data.TargetPath = item.NewDisplayName;
                            }
                            else
                            {
                                statusList.Add(new DeploymentReportImportStatus() { Message = string.Format("Error; SourceDisplayName not equals current TargetPath: [{0}]=[{1}]", item.SourceDisplayName, data.TargetPath), SourceUrl = item.SourceUrl });
                            }
                        }
                    }
                }
                else
                {
                    statusList.Add(new DeploymentReportImportStatus() { Message = "Error; DeploymentReport item not in the target tree founded", SourceUrl = key });
                }
            }
        }



        #endregion

        #region Private Methods
        private void PopulateRecursive(TreeNode node, Dictionary<string, TreeNode> treeNodes)
        {
            var data = node.Tag as DataNode;

            if (!string.IsNullOrEmpty(data.SourceUrl))
            {
                treeNodes.Add(data.SourceUrl, node);
            }

            foreach (TreeNode child in node.Nodes)
            {
                PopulateRecursive(child, treeNodes);
            }
        }
        #endregion

    }
}
